<?php

/**
 * Description of WrapperApp
 *
 * @author EaDeveloper
 */
abstract class ReportDoc {

    public $adminUser = false;
    public $paramPage;
    public $resultPage;
    public $rootPath;
    public $key;
    public $label;
    public $subtitle;
    public $db;
    public $dataPrinter;
    public $printers;
    public $lastQuery;
    public $callbackRow;
    public $callbackSql;
    public $totalPages = 0;
    public $totalRecords = 0;
    public $lastResult = null;
    public $fields = null;
    public $disablePager = false;
    public $pagerMode = false;
    public $pageLimit = 100;
    public $currentPage = 1;
    public $titulo;
    public $parametros;
    public $orientacion;
    public $tipoEjecucion;
    public static $TIPO_NUMERICO = 1;
    public static $TIPO_LETRA = 2;

    function __construct($paramPage, $resultPage, $rootPath, $key, $label, $crow = null, $csql = null, $fields = null, $orientacion = "P", $ejecucion = "D") {
        $this->fields = $fields;
        $this->callbackSql = $csql; //funcion que genera sql
        $this->callbackRow = $crow; //funcion para cada fila generada
        $this->paramPage = $paramPage;
        $this->resultPage = $resultPage;
        $this->rootPath = $rootPath;
        $this->key = $key;
        $this->label = $label;
        $this->tipoEjecucion = $ejecucion;
        $this->orientacion = $orientacion;
    }

    /**
     * @see Genera el reportes con su pantalla de parametros!
     */
    public function show() {
        $params = getParam("params");
        if (isEmpty($params)) {
            echo '<div id="maindiv">';
            include $this->rootPath . $this->paramPage;
            echo '</div>';
        } else {
            include $this->rootPath . $this->resultPage;
        }
    }

    /**
     * Genera una consulta SQL en base a los callbacks programados
     * @param type $pagMode
     * @return type 
     */
    public function procSql($pagMode = false) {
        $sqlRs = array();
        $this->pagerMode = $pagMode;
        if ($this->callbackSql == null) {
            return null;
        }
        call_user_func(array($this, $this->callbackSql), null);
        $sql = $this->lastQuery;
        if ($pagMode) {
            $page = getParam("page");
            $this->currentPage = $page;
            $limit = $this->pageLimit;
            $page = isEmpty($page) ? 1 : $page;
            $start = ($limit * $page) - $limit;

            if ($this->disablePager) {
                $sql = str_replace("%start%", $start, $sql);
                $sql = str_replace("%limite%", $limit, $sql);
                $this->lastQuery = $sql;
            }

            $sqlPager = "select * from($sql) a limit $start,$limit";
            $sqlRs = $this->db->queryToArray($sqlPager);
            $rsT = $this->db->queryToArray("select COUNT(*) TOTAL from ($sql) A");
            $c = count($rsT) > 0 ? $rsT[0]["TOTAL"] : 0;
            $c = isEmpty($c) ? 0 : $c;
            if ($pagMode) {
                $this->totalRecords = $c;
                $this->totalPages = round($c / $limit);
            }
        } else {
            $sqlRs = $this->db->queryToArray($sql);
        }
        $this->lastResult = $sqlRs;
        if ($this->callbackRow != null) {
            call_user_func(array($this, $this->callbackRow), null);
        }
        $this->pageLimit = 100;
    }

    public function generateSQL() {
        call_user_func(array($this, $this->callbackSql), null);
        $this->procSql(!$this->disablePager);
    }

    public function query($sql) {
        $this->db = DbManager::getMe();
        return $this->db->queryToArray($sql);
    }

}

?>
